package com.yksj.healthtalk.ui.home;

import org.universalimageloader.core.download.ImageDownloader;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.services.MessagePushService;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.chatting.ChatActivity;
import com.yksj.healthtalk.ui.chatting.DoctorChatActivity;

public class DoctorListActivity extends BaseFragmentActivity implements OnClickListener,OnItemClickListener{
	
	private ExpandableListView mExpandableListView;
	private int type;//类型 0医生,1助理
	private DoctorListAdapter mAdapter;
	AppData mAppData;
	
	/**
	 * 助理消息更新
	 */
	final BroadcastReceiver mReceiver = new BroadcastReceiver(){
		public void onReceive(android.content.Context context, Intent intent) {
			mAdapter.notifyDataSetChanged();
		};
	};
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_list_main);
		type = getIntent().getIntExtra("type",0);
		mAppData = HTalkApplication.getAppData();
		
		initTitle();
		
		initView();
	}
	
	private void initView() {
		mExpandableListView = (ExpandableListView) findViewById(android.R.id.list);
	}
	
	@Override
	protected void onStart() {
		IntentFilter filter = new IntentFilter(MessagePushService.ACTION_MESSAGE);
		registerReceiver(mReceiver, filter);
		super.onStart();
	}
	
	
	@Override
	protected void onResume() {
		if(type == 1){
			mAdapter.notifyDataSetChanged();
		}
		super.onResume();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		if(type == 1)unregisterReceiver(mReceiver);
	}
	
	
	
/*	public void initTitle() {
		findViewById(R.id.title).setBackgroundResource(R.drawable.setting_title_bg);
		Button backButton = (Button) findViewById(R.id.title_back);
		backButton.setOnClickListener(this);
		backButton.setText("返回");
		backButton.setBackgroundResource(R.drawable.setting_main_back_bg);
		TextView titleText = (TextView) findViewById(R.id.title_lable);
		titleText.setText(type == 0 ? "我的医生":"我的助理");
		titleText.setTextColor(getResources().getColor(R.color.black));
	}*/

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.title_back:
			this.finish();
			break;
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		switch(arg2) {
		case 0:
			Intent intent = null;
			if(type == 0){
				intent = new Intent(this,DoctorChatActivity.class);
			}else{
				AppData data = HTalkApplication.getAppData();
				boolean isContain = data.cacheInformation.containsKey(SmartFoxClient.helperId);
				CustomerInfoEntity entity = null;
				if(isContain){
					entity = (CustomerInfoEntity)data.cacheInformation.get(SmartFoxClient.helperId);
				}else{
					entity = new CustomerInfoEntity();
					entity.setName("多美助理");
					entity.setId(SmartFoxClient.helperId);
					entity.setNormalHeadIcon(ImageDownloader.PROTOCOL_FILE_ASSETS+":///customerIcons/icon_healper.png");
					data.cacheInformation.put(SmartFoxClient.helperId, entity);
				}
				intent = new Intent(this,ChatActivity.class);
				intent.putExtra(ChatActivity.KEY_PARAME,entity);
			}
			startActivity(intent);
			break;
		}
	}
	
	public class DoctorListAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return 1;
		}
		@Override
		public Object getItem(int arg0) {
			return null;
		}
		@Override
		public long getItemId(int arg0) {
			return 0;
		}
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			if(arg1==null) {
				if(type == 0){
					arg1 = LayoutInflater.from(DoctorListActivity.this).inflate(R.layout.doctor_list_item, arg2,false);
				}else{
					arg1 = LayoutInflater.from(DoctorListActivity.this).inflate(R.layout.healper_list_item, arg2,false);
					TextView textView = (TextView)arg1.findViewById(R.id.mesg_txt);
					arg1.setTag(textView);
				}
			}
			
			//助理未读消息数量
			if(type == 1){
				TextView textView = (TextView)arg1.getTag();
				String size = mAppData.getNoReadMesgSize(SmartFoxClient.helperId)+"";
				if(size == null){
					textView.setVisibility(View.GONE);
				}else{
					textView.setVisibility(View.VISIBLE);
					textView.setText(size);
				}
			}
			return arg1;
		}
	}

}
